<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Libraries\Error;
use App\Models\InfoTemplate;
use App\Models\User;
use Firebase\JWT\JWT;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TemplateController extends ApiController
{
    public function personinfo(Request $request)
    {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];

        if (empty($company_id)) {
            $list = DB::table('info_template')->where('type', 1)->where('company_id',0)->orderby('sequence','asc')->get();
        } else {
            $list = DB::table('info_template')->where('type', 1)->where('company_id',$company_id)->orderby('sequence','asc')->get();

            if ($list->isEmpty()){
                $list = DB::table('info_template')->where('type', 1)->where('company_id',0)->orderby('sequence','asc')->get();
            }

        }
        return showError(Error::ERR_OK,$list);
    }

    public function entryinfo(Request $request)
    {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        if (empty($company_id)) {
            $list = DB::table('info_template')->where('type', 2)->where('company_id',0)->orderby('sequence','asc')->get();
        } else {
            $list = DB::table('info_template')->where('type', 2)->where('company_id',$company_id)->orderby('sequence','asc')->get();

            if ($list->isEmpty()){
                $list = DB::table('info_template')->where('type', 2)->where('company_id',0)->orderby('sequence','asc')->get();
            }

        }
        return showError(Error::ERR_OK,$list);
    }

    public function editpersoninfo(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        if (empty($company_id)) {
            return showError(Error::ERR_ERROR, null, "公司ID必传!");
        }
        $list = $request->list;
//        $list = json_decode($request->list,true);
//        dd($list);
        foreach ($list as $k=>$v) {
            if ($v['id']>44) {
                $infoTemplate = InfoTemplate::findOrFail($v['id']);
            } else {
                $infoTemplate = new InfoTemplate();
            }
            $infoTemplate->cloumn_name = $v['cloumn_name'];
            $infoTemplate->english_cloumn_name = $v['english_cloumn_name'];
            $infoTemplate->is_show = $v['is_show'];
            $infoTemplate->is_title = $v['is_show'];
            $infoTemplate->is_require = $v['is_require'];
            $infoTemplate->is_modify = $v['is_modify'];
            $infoTemplate->type = 1;
            $infoTemplate->sequence = $k+1;
            $infoTemplate->company_id = $company_id;
            $infoTemplate->save();
        }

        return showError(Error::ERR_OK);

    }

    public function editentryinfo(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        if (empty($company_id)) {
            return showError(Error::ERR_ERROR, null, "公司ID必传!");
        }

        $list = $request->list;
        foreach ($list as $k=>$v) {
            if ($v['id']>44) {
                $infoTemplate = InfoTemplate::findOrFail($v['id']);
            } else {
                $infoTemplate = new InfoTemplate();
            }
            $infoTemplate->cloumn_name = $v['cloumn_name'];
            $infoTemplate->english_cloumn_name = $v['english_cloumn_name'];
            $infoTemplate->is_show = $v['is_show'];
            $infoTemplate->is_title = $v['is_show'];
            $infoTemplate->is_require = $v['is_require'];
            $infoTemplate->is_modify = $v['is_modify'];
            $infoTemplate->type = 2;
            $infoTemplate->sequence = $k+1;
            $infoTemplate->company_id = $company_id;
            $infoTemplate->save();
        }

        return showError(Error::ERR_OK);

    }

    public function updateTitle(Request $request) {
        $list = $request->list;
        if (empty($list)) {
            return showError(Error::ERR_ERROR, null, "list参数必传!");
        }
        foreach ($list as $k=> $v) {
            InfoTemplate::where('id',$v['id'])->update(['is_title'=>$v['is_title'],'title_sequence'=>$k+1]);
        }
        return showError(Error::ERR_OK);
    }

    public function getTitle(Request $request)
    {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];

        $list = DB::table('info_template')->where('is_title',1)->where('company_id',$company_id)->orderby('title_sequence','asc')->get();

        return showError(Error::ERR_OK,$list);
    }



}
